package com.projectsquirrel;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class NewObservation4 extends Activity  {
	
	private static final int PREFERENCES_GROUP_ID = 11;
	private static final int PAGE_2 = 12;
	private static final int PAGE_3 = 13;
	private static final int PAGE_4 = 14;
	private static final int PAGE_5 = 15;
	private static final int PAGE_6 = 16;
	private static final int PAGE_7 = 17;
	
	private LocationManager myManager;

	private Observation newObs;
	    
	private RadioGroup num_Squirrels_Group;
    private Button nextObsButton2;
    private TextView completitionStatus;
    private Context context;
    private Spinner settingSpinner;
    private SharedPreferences pref;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.observationformcd);//layout 1.5
	    ImageButton infoIconButton = (ImageButton) findViewById(R.id.infoScreenIcon);
	    ImageButton settingsIconButton = (ImageButton) findViewById(R.id.settingsScreenIcon);
	     
	    num_Squirrels_Group = (RadioGroup) findViewById(R.id.num_Squirrels_Group);
	    nextObsButton2 = (Button) findViewById(R.id.nextObsScreen2);
	    ArrayList<String> SettingList = new ArrayList<String>();
	    SettingList.add("Single-family home");
	    SettingList.add("Apartment Building");
	    SettingList.add("Highrise");
	    SettingList.add("Neighborhood Park");
	    SettingList.add("Forest/Nature Preserve");
	    SettingList.add("Zoo/Arboretum/Botanical Garden");
	    SettingList.add("Commercial Establishment");
	    SettingList.add("School Campus");
	    SettingList.add("Vacant Lot");
	    SettingList.add("Cemetery");
	    SettingList.add("Farmland");
	    SettingAdapter contactadapter = new SettingAdapter(this, SettingList);
	    
	    settingSpinner = (Spinner) findViewById(R.id.settingSpinner);
	    completitionStatus = (TextView) findViewById(R.id.textView2);
	    context = getApplicationContext();
	    pref = getSharedPreferences("AppSettings",MODE_PRIVATE);
	    completitionStatus.setText("48% complete");
	  	    
	    
	    //setup the gps manager
	    myManager = (LocationManager) getSystemService(LOCATION_SERVICE);	
	    String email = pref.getString("userEmail", null);
	    newObs = Observation.getInstance(email);
	    
	    settingSpinner.setAdapter(contactadapter);
	    
	    String setting = newObs.getSetting();
	    if(setting.equals("SINGLE_FAM")) settingSpinner.setSelection(0);
	    if(setting.equals("MULT_FAM")) settingSpinner.setSelection(1);
	    if(setting.equals("HIGHRISE")) settingSpinner.setSelection(2);
	    if(setting.equals("PARK")) settingSpinner.setSelection(3);
	    if(setting.equals("NATURE")) settingSpinner.setSelection(4);
	    if(setting.equals("GARDEN")) settingSpinner.setSelection(5);
	    if(setting.equals("COMMER")) settingSpinner.setSelection(6);
	    if(setting.equals("SCHOOL")) settingSpinner.setSelection(7);
	    if(setting.equals("LOT")) settingSpinner.setSelection(8);
	    if(setting.equals("CEMETERY")) settingSpinner.setSelection(9);
	    if(setting.equals("FARM")) settingSpinner.setSelection(10);
	    
	    String numSqr = newObs.getNumOfSqurChange();
	    if(numSqr.equals("SAME")) 
	    	((RadioButton) findViewById(R.id.num_Squirrels_Same)).setChecked(true);
	    if(numSqr.equals("INCREASED")) 
	    	((RadioButton) findViewById(R.id.num_Squirrels_Increased)).setChecked(true);
	    if(numSqr.equals("DECREASED")) 
	    	((RadioButton) findViewById(R.id.num_Squirrels_Decreased)).setChecked(true);
	
	    settingSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
		    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		        //newObs.setSetting(((String) parent.getItemAtPosition(pos)).toUpperCase());
		    	//SETTING, "[X, where X is one of: SINGLE_FAM, MULT_FAM, HIGHRISE, PARK, NATURE, GARDEN, COMMER, SCHOOL, LOT, CEMETERY, FARM]"
		    	switch(pos){
		    	case 0:
		    		newObs.setSetting("SINGLE_FAM");
		    		break;
		    	case 1:
		    		newObs.setSetting("MULT_FAM");
		    		break;
		    	case 2:
		    		newObs.setSetting("HIGHRISE");
		    		break;
		    	case 3:		
		    		newObs.setSetting("PARK");
		    		break;
		    	case 4:
		    		newObs.setSetting("NATURE");
		    		break;
		    	case 5:
		    		newObs.setSetting("GARDEN");
		    		break;
		    	case 6:
		    		newObs.setSetting("COMMER");
		    		break;
		    	case 7:
		    		newObs.setSetting("SCHOOL");
		    		break;
		    	case 8:
		    		newObs.setSetting("LOT");
		    		break;
		    	case 9:
		    		newObs.setSetting("CEMETERY");
		    		break;
		    	case 10:
		    		newObs.setSetting("FARM");
		    		break;
		    	
		    	}
		    }
		    public void onNothingSelected(AdapterView<?> parent) {
		    }
		});
	    
	    // Create radio groups
	    
	   
	    num_Squirrels_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {	        	
	        	switch (checkedID){
	        	case R.id.num_Squirrels_Same:
	        		newObs.setNumOfSqurrelChange("SAME");
	        		break;
	        	case R.id.num_Squirrels_Increased:
	        		newObs.setNumOfSqurrelChange("INCREASED");
	        		break;
	        	case R.id.num_Squirrels_Decreased:
	        		newObs.setNumOfSqurrelChange("DECREASED");
	        		break;
	        	default: break;
	        	}     	
	        }
	    });
	    
	  



	    infoIconButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation4.this, infoMenu.class);
		        startActivity(i);
			}	    	
	    });
	    
	    settingsIconButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation4.this, settingsMenu.class);
		        startActivity(i);
			}	    	

	    });

	    
	  
	    nextObsButton2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation4.this, NewObservation5.class);
		        startActivity(i);
				//finish();
			}
			
	    });

	 

	
     }
     	@Override
	protected void onStart(){
     	// for the dialog indicating the gps is disabled
		super.onStart();
	}
	@Override
	protected void onDestroy() {
		//stopListening();
		super.onDestroy();
	}
	
	@Override
	protected void onPause() {
		//stopListening();
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		//startListening();
		super.onResume();
	}
	

	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK) ) {
	        
	    }
	    return super.onKeyDown(keyCode, event);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
	super.onCreateOptionsMenu(menu);

	menu.add(PREFERENCES_GROUP_ID, PAGE_2, 0, "Date/Time")
	.setIcon(R.drawable.menuicondatetime);
	menu.add(PREFERENCES_GROUP_ID, PAGE_3, 0, "General")
	.setIcon(R.drawable.menuicongencom);
	menu.add(PREFERENCES_GROUP_ID, PAGE_4, 0, "Setting")
	.setIcon(R.drawable.menuiconsetting);
	menu.add(PREFERENCES_GROUP_ID, PAGE_5, 0, "Trees")
	.setIcon(R.drawable.menuicontree);
	menu.add(PREFERENCES_GROUP_ID, PAGE_6, 0, "Food")
	.setIcon(R.drawable.menuiconfood);
	menu.add(PREFERENCES_GROUP_ID, PAGE_7, 0, "Predators")
	.setIcon(R.drawable.menuiconpred);

	return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		Intent i;
		switch (item.getItemId()) {
		case PAGE_2:
			i = new Intent(NewObservation4.this, NewObservation2.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_3:
			i = new Intent(NewObservation4.this, NewObservation3.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_4:
			return true;
		case PAGE_5:
			i = new Intent(NewObservation4.this, NewObservation5.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_6:
			i = new Intent(NewObservation4.this, NewObservation6.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_7:
			i = new Intent(NewObservation4.this, NewObservation7.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		}
		return true;
	}
	
	
}

